<?php


namespace RedMVC\Controller;
use RedMVC;

/**
 * AController class
 *  
 * @category    RedMVC
 * @package     Controller
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
abstract class AController{

    protected $request;
    protected $response;
    protected $view;
    protected $layout;    
    protected $db;
    
    protected $viewEnabled      = true;
    protected $layoutEnabled    = true;

    public function init(){}
    
    public function setRequest($request){
        $this->request = $request;
    }
    
    public function setResponse($response){
        $this->response = $response;
    }

    public function loadView($viewScript){
        $viewPath       = APP_PATH.DS.'views'.DS.'scripts'.DS;
        $viewScript     = $viewPath.$viewScript;
        if(file_exists($viewScript)){
            $this->view = new RedMVC\View($viewScript);
            RedMVC\Registry::get('RedMVC_ViewHelper')->setView($this->view);
        }
        else{            
            throw new \Exception('View '.$viewScript.' doesn\'t exist.');
        }
    }
    
    public function setView($view){
        $this->view = $view;
    }

    public function getView(){
        return $this->view;
    }
    
    public function setDb($db){
        $this->db = $db;
    }

    public function loadLayout($layoutScript){
        $layoutPath     = APP_PATH.DS.'layouts'.DS;
        $layoutScript   = $layoutPath.$layoutScript;
        if(file_exists($layoutScript)){
            $layout = new RedMVC\Layout($layoutScript);
        }
        else{
            throw new \Exception('Layout '.$layoutScript.' doesn\'t exist.');
        }
        $this->layout = $layout;
        RedMVC\Registry::set('RedMVC_Layout', $layout);
    }
    
    public function getLayout(){
        return $this->layout;
    }
    
    public function setViewEnabled($value){
        $this->viewEnabled = $value;
    }
    
    public function getViewEnabled(){
        return $this->viewEnabled;
    }
    
    public function setLayoutEnabled($value){
        $this->layoutEnabled = $value;
    }
    
    public function getLayoutEnabled(){
        return $this->layoutEnabled;
    }
    
    public function redirect($controller, $action, $params = array(), $absoluteUrl = false){
        Front::redirect($controller, $action, $params, $absoluteUrl);
    }
    
    public function redirectUrl($url){
        Front::redirectUrl($url);
    }
    
    public function url($controller, $action, $params = array(), $absoluteUrl = false){
        return Front::url($controller, $action, $params, $absoluteUrl);
    }
    
    public function selfUrl($paramsToExclude = true, $params = array(), $absoluteUrl = false){
        return Front::selfUrl($paramsToExclude, $params, $absoluteUrl);
    }
    
    public static function buildUrl($controller, $action, $params = array()){        
        return Front::buildUrl($controller, $action, $params);
    }

}